<?php

class AppraisalsController extends AppController {

    var $name = 'Appraisals';

    function index() {
        $id = $this->Session->read('proy_id');
        $cod = $this->Session->read('cod');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } elseif ($this->Session->read('fase1') != 1) {
            $this->Session->setFlash('Este Proyecto No Superó Fase Uno');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        } else {
            $this->Appraisal->recursive = -1;
            $avaluo = $this->Appraisal->find('first', array('conditions' => array('Appraisal.proyect_id' => $id), 'fields' => array('Appraisal.id', 'Appraisal.bloqueo', 'Appraisal.fecha_radicacion', 'Appraisal.tipo_inmueble', 'Appraisal.plancha_igac', 'Appraisal.destinacion_actual', 'Appraisal.fecha_asignacion', 'Appraisal.fecha_visita', 'Appraisal.radicacion')));
            $this->Session->write('bloqueo_avaluo', $avaluo['Appraisal']['bloqueo']);
            $this->set("avaluo", $avaluo);
            $this->set("proy_id", $id);
            $this->set("codigo", $cod);
        }
    }

    function add() {

        $id = $this->Session->read('proy_id');

        $this->set('id', $id);
        $this->set('cod', $this->Session->read('cod'));

        if (!empty($this->data)) {

            App::import('Model', 'PropertyAppraisal');
            App::import('Model', 'Property');

            $property_appraisal = new PropertyAppraisal();
            $predio = new Property();


            if ($this->Appraisal->saveAll($this->data)) {
                $this->Session->setFlash('Avaluo guardado con exito');


                $predios = $predio->find('all', array('conditions' => array('Property.proyect_id' => $id), 'fields' => array('Property.id')));
                $idAppraisal = $this->Appraisal->id;
                foreach ($predios as $predio1) {
                    $property_appraisal->create();
                    $array = array(
                        'appraisal_id' => $idAppraisal,
                        'property_id' => $predio1['Property']['id']
                    );

                    $property_appraisal->save($array);
                }


                $this->redirect(array('controller' => 'Appraisals', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Error guardando datos');
            }
        } else {
            
        }
    }

    function edit($id) {
        $this->set('cod', $this->Session->read('cod'));
        $this->set('proy_id', $this->Session->read('proy_id'));

        $this->Appraisal->recursive = -1;

        if (empty($this->data)) {
            //devuelve los datos que hay en la base de datos
            $this->data = $this->Appraisal->find("first", array("conditions" => array("Appraisal.id" => $id), 'fields' => array('Appraisal.id', 'Appraisal.bloqueo', 'Appraisal.fecha_radicacion', 'Appraisal.tipo_inmueble', 'Appraisal.plancha_igac', 'Appraisal.destinacion_actual', 'Appraisal.fecha_asignacion', 'Appraisal.fecha_visita', 'Appraisal.radicacion')));
        } else {
            //guarda los datos modificados
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Avalúo actualizado con exito');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'index'));
            } else {
                //si no fueron guardados 
                $this->Session->setFlash('Error guardando datos');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'index'));
            }
        }
    }

    function docs_sum($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.documentos_suministrados', 'Appraisal.id', 'Appraisal.bloqueo')));
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'docs_sum_index', $id));
            }
        }
    }

    function docs_sum_index($id) {
        $this->Appraisal->recursive = -1;
        $documentos = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.documentos_suministrados', 'Appraisal.id')));
        $this->set('documentos', $documentos);
    }

    function sector_description($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.delimitacion_sector', 'Appraisal.id', 'Appraisal.actividad_predominante', 'Appraisal.desarrollo', 'Appraisal.nivel_socio_economico', 'Appraisal.comercializacion', 'Appraisal.vias_acceso', 'Appraisal.servicios_comunales', 'Appraisal.servicios_publicos', 'Appraisal.orden_publico', 'Appraisal.perspectivas_valorizacion', 'Appraisal.bloqueo')));
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'sector_description_index', $id));
            }
        }
    }

    function sector_description_index($id) {
        $this->Appraisal->recursive = -1;
        $description = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.delimitacion_sector', 'Appraisal.id', 'Appraisal.actividad_predominante', 'Appraisal.desarrollo', 'Appraisal.nivel_socio_economico', 'Appraisal.comercializacion', 'Appraisal.vias_acceso', 'Appraisal.servicios_comunales', 'Appraisal.servicios_publicos', 'Appraisal.orden_publico', 'Appraisal.perspectivas_valorizacion')));
        $this->set('description', $description);
    }

    function zoning($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.reglamentacion_urbanistica', 'Appraisal.id', 'Appraisal.bloqueo')));
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'zoning_index', $id));
            }
        }
    }

    function zoning_index($id) {
        $this->Appraisal->recursive = -1;
        $zona = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.reglamentacion_urbanistica', 'Appraisal.id')));
        $this->set('zona', $zona);
    }

    function generals_ground($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.bloqueo', 'Appraisal.id', 'Appraisal.ubicacion', 'Appraisal.piso_termico', 'Appraisal.suelos', 'Appraisal.cercas_perimetrales_internas', 'Appraisal.explotacion_economica', 'Appraisal.redes_de_servicios', 'Appraisal.irrigacion', 'Appraisal.lindero_norte', 'Appraisal.lindero_sur', 'Appraisal.lindero_oriente', 'Appraisal.lindero_occidente', 'Appraisal.distancia_perimetro_urbano', 'Appraisal.altura_maxima', 'Appraisal.altura_minima', 'Appraisal.temperatura_maxima', 'Appraisal.temperatura_minima', 'Appraisal.temperatura_promedio', 'Appraisal.precipitacion')));
            App::Import('model', 'Poll');
            $poll = new Poll();
            $poll->recursive = -1;
            $str = "\n";
            //eliminacion de campo inutil. OA
            $encuesta = $poll->find('first', array('conditions' => array('Poll.proyect_id' => $this->Session->read('proy_id')), 'fields' => array('Poll.piso_termico', 'Poll.vias_acceso', 'Poll.tiene_vias_internas', 'Poll.descripcion_vias_internas', 'Poll.distancia_centro_urbano_km', 'Poll.distancia_centro_urbano_horas', 'Poll.distancia_via_principal_km', 'Poll.distancia_via_principal_horas', 'Poll.lindero_norte', 'Poll.lindero_sur', 'Poll.lindero_oriente', 'Poll.lindero_occidente', 'Poll.altura_minima_predio', 'Poll.altura_maxima_predio', 'Poll.temperatura_promedio', 'Poll.temperatura_maxima', 'Poll.temperatura_minima', 'Poll.precipitacion', 'Poll.piso_termico')));

            if ($this->data['Appraisal']['ubicacion'] == "") {
                $this->data['Appraisal']['ubicacion'] = "VIAS DE ACCESO:\n" . $encuesta['Poll']['vias_acceso'] . "\n" . $encuesta['Poll']['descripcion_vias_internas'] . "\nDistancia centro_urbano en km:" . $encuesta['Poll']['distancia_centro_urbano_km'] . " " . "Distancia centro urbano en horas:" . $encuesta['Poll']['distancia_centro_urbano_horas'] . "\nDistancia via principal en km: " . $encuesta['Poll']['distancia_via_principal_km'] . " Distincia via principal en horas: " . $encuesta['Poll']['distancia_via_principal_horas'];
            }
            if ($this->data['Appraisal']['altura_maxima'] == "") {
                $this->data['Appraisal']['altura_maxima'] = $encuesta['Poll']['altura_maxima_predio'];
            }
            if ($this->data['Appraisal']['altura_minima'] == "") {
                $this->data['Appraisal']['altura_minima'] = $encuesta['Poll']['altura_minima_predio'];
            }
            if ($this->data['Appraisal']['temperatura_minima'] == "") {
                $this->data['Appraisal']['temperatura_minima'] = $encuesta['Poll']['temperatura_maxima'];
            }
            if ($this->data['Appraisal']['temperatura_maxima'] == "") {
                $this->data['Appraisal']['temperatura_maxima'] = $encuesta['Poll']['temperatura_maxima'];
            }
            if ($this->data['Appraisal']['temperatura_promedio'] == "") {
                $this->data['Appraisal']['temperatura_promedio'] = $encuesta['Poll']['temperatura_promedio'];
            }
            if ($this->data['Appraisal']['piso_termico'] == "") {
                $this->data['Appraisal']['piso_termico'] = $encuesta['Poll']['piso_termico'];
            }
            if ($this->data['Appraisal']['precipitacion'] == "") {
                $this->data['Appraisal']['precipitacion'] = $encuesta['Poll']['precipitacion'] . "\n\n";

                // , 'Poll.temperatura_maxima', 'Poll.temperatura_minima'
            }
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'generals_ground_index', $id));
            }
        }
    }

    function generals_ground_index($id) {
        $this->Appraisal->recursive = -1;
        $aspectos = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.id', 'Appraisal.ubicacion', 'Appraisal.piso_termico', 'Appraisal.suelos', 'Appraisal.cercas_perimetrales_internas', 'Appraisal.explotacion_economica', 'Appraisal.redes_de_servicios', 'Appraisal.irrigacion', 'Appraisal.lindero_norte', 'Appraisal.lindero_sur', 'Appraisal.lindero_oriente', 'Appraisal.lindero_occidente', 'Appraisal.distancia_perimetro_urbano', 'Appraisal.altura_maxima', 'Appraisal.altura_minima', 'Appraisal.temperatura_maxima', 'Appraisal.temperatura_minima', 'Appraisal.temperatura_promedio', 'Appraisal.precipitacion')));
        $this->set('aspectos', $aspectos);
    }

    function print_letter($id) {
         
        $this->set('id', $id);
        $this->layout = 'pdf';
        $this->set('usuario', $this->Auth->user('nombre') . " " . $this->Auth->user('primer_apellido') . " " . $this->Auth->user('segundo_apellido'));
    }

    function metodo_avaluo($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.bloqueo', 'Appraisal.metodo_reposicion', 'Appraisal.metodo_comparacion', 'Appraisal.expertos_avaluadores', 'Appraisal.id')));
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'method_index', $id));
            }
        }
    }

    function method_index($id) {
        $this->Appraisal->recursive = -1;
        $metodo = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.metodo_reposicion', 'Appraisal.metodo_comparacion', 'Appraisal.expertos_avaluadores', 'Appraisal.id')));
        $this->set('metodo', $metodo);
    }

    function backgrounds($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.antecedentes', 'Appraisal.bloqueo', 'Appraisal.id')));
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'backgrounds_index', $id));
            }
        }
    }

    function backgrounds_index($id) {
        $this->Appraisal->recursive = -1;
        $antecedente = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.antecedentes', 'Appraisal.id')));
        $this->set('antecedente', $antecedente);
    }

    function considerations($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.consideraciones_generales', 'Appraisal.id', 'Appraisal.bloqueo')));
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'considerations_index', $id));
            }
        }
    }

    function considerations_index($id) {
        $this->Appraisal->recursive = -1;
        $consideracion = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.consideraciones_generales', 'Appraisal.id')));
        $this->set('consideracion', $consideracion);
    }

    function economic_index($id) {
        $this->Appraisal->recursive = -1;
        $economico = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.investigacion_economica', 'Appraisal.id')));
        $this->set('economico', $economico);
    }

    function economic_edit($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.investigacion_economica', 'Appraisal.bloqueo', 'Appraisal.id')));
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'economic_index', $id));
            }
        }
    }

    function observations($id) {
        $this->Appraisal->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.bloqueo', 'Appraisal.observaciones', 'Appraisal.id')));
        } else {
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'observations_index', $id));
            }
        }
    }

    function observations_index($id) {
        $this->Appraisal->recursive = -1;

        $observacion = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.observaciones', 'Appraisal.id')));
        $this->set('observacion', $observacion);
    }

    function aprobation_index($id) {
        $this->Appraisal->recursive = -1;
        $aprobacion = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.aprobacion', 'Appraisal.concepto_aprobacion', 'Appraisal.id')));
        $this->set('aprobacion', $aprobacion);
    }

    function aprobation($id) {
        $this->Appraisal->recursive = -1;
        $this->set('grupo', $this->Auth->user('group_id'));
        if (empty($this->data)) {
            $this->data = $this->Appraisal->find('first', array('conditions' => array('Appraisal.id' => $id), 'fields' => array('Appraisal.aprobacion', 'Appraisal.bloqueo', 'Appraisal.concepto_aprobacion', 'Appraisal.id')));
        } else {
            if ($this->data['Appraisal']['aprobacion'] != "Sin registro") {
                $this->data['Appraisal']['bloqueo'] = "Si";
            } else {
                $this->data['Appraisal']['bloqueo'] = "No";
            }
            if ($this->Appraisal->save($this->data)) {
                $this->Session->setFlash('Datos Actualizados');
                $this->redirect(array('controller' => 'Appraisals', 'action' => 'index', $id));
            }
        }
    }

}

?>
